import axios from "axios"
import { Message } from 'element-ui';
const newAxios = axios.create({
    baseURL: "http://http://jacklv.cn:3001/",
    timeout: 3000
})
newAxios.interceptors.request.use((req) => {
    

    if (localStorage.token) {
        req.headers.Authorization = localStorage.token
    }
    return req;
}, () => {
    
})


newAxios.interceptors.response.use((resp) => {
   
    //后端数据传递回来特殊格式，你需要同意处理
    return resp;
}, (error) => {
    const response = error.response
    if (response) {
        switch (response.status) {
            case 500:
                Message.error("服务器异常")
                break;
            case 401:
                Message.error("身份认证过期")
                localStorage.removeItem("token")
                localStorage.removeItem('username')
                localStorage.removeItem("userInfo")
                //页面调整
                window.location.assign("/login")
                break;
        }
    }
    
})

export default newAxios